/*
 *   Copyright 2021 zzh
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */

package com.aduib.boot.common.buffer;

import com.aduib.boot.common.exception.IoException;

import java.io.OutputStream;
import java.nio.channels.WritableByteChannel;
import java.nio.charset.Charset;

/**
 * @description: BufferSink
 * @author: zzh
 * @date: 2021/11/22 10:30
 */
public interface BufferedSink extends Sink, WritableByteChannel {
  /**
   * 返回buffer
   *
   * @return
   */
  Buffer buffer();

  /**
   * 返回输出流
   *
   * @return
   */
  OutputStream outputStream();

  /** Like {@link OutputStream#write(byte[])}, this writes a complete byte array to this sink. */
  BufferedSink write(byte[] source) throws IoException;

  /**
   * Like {@link OutputStream#write(byte[], int, int)}, this writes {@code byteCount} bytes of
   * {@code source}, starting at {@code offset}.
   */
  BufferedSink write(byte[] source, int offset, int byteCount) throws IoException;

  /**
   * Removes all bytes from {@code source} and appends them to this sink. Returns the number of
   * bytes read which will be 0 if {@code source} is exhausted.
   */
  long writeAll(Source source) throws IoException;

  /** Removes {@code byteCount} bytes from {@code source} and appends them to this sink. */
  BufferedSink write(Source source, int byteCount) throws IoException;

  BufferedSink writeStringUtf8(String source) throws IoException;

  /** Encodes {@code string} in {@code charset} and writes it to this sink. */
  BufferedSink writeString(String source, Charset charset) throws IoException;

  /**
   * Writes a byte to this sink.
   *
   * @param b
   */
  BufferedSink writeByte(byte b) throws IoException;

  BufferedSink writeShort(short s) throws IoException;

  BufferedSink writeInt(int i) throws IoException;

  BufferedSink writeLong(long v) throws IoException;

  BufferedSink writeFloat(float v) throws IoException;

  BufferedSink writeDouble(double v) throws IoException;
}
